package com.shengyin.ec.controller.permission;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.shengyin.ec.model.entity.Users;
import com.shengyin.ec.service.permission.AccountService;

@Controller
public class AccountController {
	
	
	@Autowired
	AccountService accountService;
/*
 * 页面跳转 start
 * =============================================================
 * */	
	@RequestMapping(value="/admin2/permission/toAccountAdd.do")
	public String toAccountAdd(){
		return "redirect:accountAdd.jsp";
	}
	

	@RequestMapping(value="/admin2/permission/toAccountList.do")
	public String toAccountList(){
		return "redirect:accountList.jsp";
	}
	
	
	@RequestMapping(value="/admin2/permission/toAccountModify.do")
	public String toAccountModify(){
		return "redirect:accountModify.jsp";
	}
	
	/*
	 * 
	 * =============================================================
	 * */
	

	@RequestMapping(value="/admin2/permission/accountAdd.do")
	public String ResourceAdd(AccountForm accountForm) {
		
		Users account =new Users();
		
		try {
			BeanUtils.copyProperties(account, accountForm);
			accountService.createUsers(account);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		return "redirect:toUsersAdd.do";
	}

	

	@RequestMapping(value="/admin2/permission/accountList.do")
	public String ResourceList(Model model) {
		List<Users> account=accountService.getUsersList(0, 1000);
		model.addAttribute("roleList", account);
		return "/admin2/permission/accountList";
	}
	
	@RequestMapping(value="/admin2/permission/accountDelete.do")
	public String ResourceDelete(@RequestParam(value="id")long id,Model model) {
		accountService.deleteUsersById(id);
		return "redirect:toUsersList.do";
	}
	
	
	
}
